<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Jhub1_Common\Entity;

use Jhub1_Common\Model\Endpoint\Prototype\Integer;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Jhub1_Common\Model\SettingsObjects\AgentLocalConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\AgentRemoteConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\DateTimeObject;
use Jhub1_Common\Model\SettingsObjects\JSONable;

/**
 * @ORM\Entity
 * @ORM\Table(name="jhoEndpointTypes", indexes={@ORM\Index(name="uuid_index", columns={"id"})})
 */
class EndpointTypes extends POBase
{

    /**
     * @ORM\Column(type="string", name="name", length=50, nullable=false);
     * @var string
     * Short name for the endpoint
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     * @var string
     * Representation icon
     */
    private $icon;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     * @var string
     * Associated colour
     */
    private $color;

    /**
     * @ORM\Column(type="string", name="shortdescription", length=250, nullable=true);
     * @var string
     * Short description of the endpoint strictly text only created by user on the add endpoint page
     */
    private $description;

    /**
     * @ORM\Column(type="text", name="longdescription",  nullable=true);
     * @var string
     * Long description of the endpoint intended for rich, html description
     */
    private $longDescription;

    /**
     * @ORM\Column(type="integer", name="sorder", nullable=false);
     * @var integer
     * Order on local list - this could be use to click and drag order change
     */
    private $sOrder;

    /**
     * @ORM\Column(type="string", length=250, nullable=false)
     * @var string
     * Stores the class of the endpoint
     */
    private $class;

    /**
     * @ORM\Column(type="text", nullable=false)
     * @var string
     * For serialized configuration parameters, instance of the $class serialized
     */
    private $valueParamsDef;

    /**
     * @ORM\ManyToOne(targetEntity="Users")
     * @var Users
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * })
     * Owner of the type
     */
    private $user;

    public function __construct(Users $user)
    {
        $this->setUser($user);
        $this->setOrder(0);
        $this->setCreated(new DateTimeObject());
    }

    /** @param Users $user
     *  @throws InvalidArgumentException
     */
    public function setUser(Users $user)
    {
        if ($user === null || $user instanceof Users) {
            $this->user = $user;
        } else {
            throw new InvalidArgumentException('$user must be instance of Users or null!');
        }
    }

    public function getUser()
    {
        return $this->user;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($val)
    {
        $this->name = $val;
    }

    public function getIcon()
    {
        return $this->icon;
    }

    public function setIcon($val)
    {
        $this->icon = $val;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function setColor($val)
    {
        $this->color = $val;
    }

    public function getOrder()
    {
        return $this->sOrder;
    }

    public function setOrder($val)
    {
        $this->sOrder = $val;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setLongDescription($val)
    {
        $this->longDescription = $val;
    }

    public function getLongDescription()
    {
        return $this->longDescription;
    }

    public function setDescription($val)
    {
        $this->description = $val;
    }

    public function getClass()
    {
        return $this->class;
    }

    public function setClass($val)
    {
        $this->class = $val;
    }

    public function getValueParamsDef()
    {
        $configuration = new AgentRemoteConfigurationObject();
        $configuration->fromJSON($this->valueParamsDef);
        return $configuration;
    }

    public function setValueParamsDef(JSONable $typeObject)
    {
        $this->valueParamsDef = $typeObject->toJSON();
    }

    public function toString()
    {
        $string           = '---------------------------------------------'. PHP_EOL;
        $string = $string . 'ENDPOINT TYPE OBJECT:                     ' . PHP_EOL;
        $string = $string . '---------------------------------------------'. PHP_EOL;
        $string = $string . 'ID:                  ' . $this->getId() . PHP_EOL;
        $string = $string . 'Name:                ' . $this->name . PHP_EOL;
        $string = $string . 'Description:         ' . $this->description . PHP_EOL;
        $string = $string . 'Icon:                ' . $this->icon . PHP_EOL;
        $string = $string . 'Colour:              ' . $this->color . PHP_EOL;
        $string = $string . 'Order:               ' . $this->sOrder . PHP_EOL;
        $string = $string . 'Created:             ' . $this->getCreated()->toString() . PHP_EOL;
        $string = $string . 'Updated:             ' . $this->getUpdated()->toString() . PHP_EOL;
        $string = $string . 'Class:               ' . $this->class . PHP_EOL;
        $string = $string . 'Config:              ' . PHP_EOL . $this->valueParamsDef . PHP_EOL;
        $string = $string . '---------------------------------------------'. PHP_EOL;
        $string = $string . 'OWNER:                     ' . PHP_EOL;
        $string = $string . $this->getUser()->toString() . PHP_EOL;
        return $string;
    }
}
